<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            width: 600px;
            height: 600px;
            background-color: blue;
        }

        .son {
            width: 300px;
            height: 150px;
            border: 1px solid black;
            background-color: aqua;
        }
    </style>

</head>

<body>
    <div id="app">
        <son-com v-for="(item,index) in product" :title="item.title" :price="item.price" :key="index" :index="index"
            @myclick="fn"></son-com>
    </div>
</body>

</html>
<script src="./vue.js"></script>
<script>

    /* 
    数据是来自父元素，点击砍价时，在修改父元素的数据
    */
    Vue.component("sonCom", {
        template: `<div class="son">
        <h3>标题：{{title}}</h3>
        <p>价格：{{price}}元</p>
        <p>开业大酬宾，全场八折</p>
        <button @click="kanDao">点我砍价</button>
        </div>`,
        props: ['title', 'price', 'index'],
        data() {
            return {

            }
        },
        methods: {
            kanDao() {
                //避免直接更改组件的属性，因为每当父组件重新渲染时，该值将被覆盖
                //this.price--;//不合适
                //遵循谁的数据谁来改
                //console.log(this.index);//子传父，传递是下标，告知父元素我点击的是你的哪一项
                this.$emit("myclick", this.index, 3)
            }
        }

    })


    const vm = new Vue({
        el: "#app",
        data: {
            product: [
                { title: "超级好吃的口水鸡", price: 50 },
                { title: "超级好吃的口榴莲", price: 100 },
                { title: "超级好吃的水果", price: 70 },
            ]
        },
        methods: {
            fn(index, val) {
                this.product[index].price -= val
            }
        }
    })
</script>